package com.aistar.service.user.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.user.EmployeeCollectionAndCompamyAndRecruitmentDTO;
import com.aistar.model.entity.*;
import com.aistar.service.user.IMycollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MycollectionServiceImpl implements IMycollectionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeCollectionMapper employeeCollectionMapper;
    @Autowired
    private RecruitmentMapper recruitmentMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private DeliveryMapper deliveryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<EmployeeCollectionAndCompamyAndRecruitmentDTO> collection(Integer userId) {
        String stat=null;
        List<EmployeeCollectionAndCompamyAndRecruitmentDTO> lists = new ArrayList<>();
        List<Delivery> deliveryRecruitmentList=new ArrayList<>();
        List<EmployeeCollection> collectionRescruitmentList=new ArrayList<>();

        for (Integer collection:employeeCollectionMapper.findAllByEmployeeId(employeeMapper.findNameByUserId(userId).getId())){
            collectionRescruitmentList.add(employeeCollectionMapper.findById(collection));
        }
        for(Resume resume:resumeMapper.findByEmployeeId(employeeMapper.findNameByUserId(userId).getId())){
            for (Delivery delivery:deliveryMapper.findByResumeId(resume.getId())){
                deliveryRecruitmentList.add(delivery);
            }
        }
        for (int i=0;i<collectionRescruitmentList.size();i++){
            for (int j=0;j<deliveryRecruitmentList.size();j++){
                if (deliveryRecruitmentList.get(j).getRecruitmentId()==collectionRescruitmentList.get(i).getRecruitmentId()){
                    stat="已投递";
                    break;
                }else {
                    stat="未投递";
                }
            }
            EmployeeCollectionAndCompamyAndRecruitmentDTO employeeCollectionAndCompamyAndRecruitmentDTO=new EmployeeCollectionAndCompamyAndRecruitmentDTO();
            employeeCollectionAndCompamyAndRecruitmentDTO.setRecruitment(recruitmentMapper.findById(collectionRescruitmentList.get(i).getRecruitmentId()));
            employeeCollectionAndCompamyAndRecruitmentDTO.setCompany(companyMapper.findById(recruitmentMapper.findById(collectionRescruitmentList.get(i).getRecruitmentId()).getId()));
            employeeCollectionAndCompamyAndRecruitmentDTO.setStat(stat);
            lists.add(employeeCollectionAndCompamyAndRecruitmentDTO);
        }
        return lists;
    }

    @Override
    public Integer deleteCollection(Integer employeeId, Integer recruitmentId) {
        return employeeCollectionMapper.deleteByEidAndRid(employeeId,recruitmentId);
    }




}
